package cn.marinda.resturants.service.impl;

import cn.marinda.resturants.entity.Meal;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.mapper.MealMapper;
import cn.marinda.resturants.service.MealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/** 菜品服务实现类
 * @date 2021/11/17
 * @author Marinda
 */
@Service("mealService")
public class MealServiceImpl extends ServiceImpl<MealMapper,Meal> implements MealService {

//    定义mapper注入
    @Resource

    public MealMapper mealMapper;


    /**
     * 分页查询meal
     *
     * @param pages
     */
    @Override
    public IPage<Meal> selectPages(Pages<?> pages) {
        IPage<Meal> mealIPage= mealMapper.selectPages(pages);
        return mealIPage;
    }

    /**
     * 获取所有菜品数据
     *
     * @return 列表菜品
     */
    @Override
    public List<Meal> queryList() {
        return mealMapper.selectList(null);
    }

    /**
     * 指定uid查询
     *
     * @param uid uid
     * @return 菜品
     */
    @Override
    public Meal selectById(String uid) {
        return mealMapper.selectByUid(uid);
    }

    /**
     * 模糊查询名字获取菜品列表
     *
     * @param name 名称
     * @return 菜品列表
     */
    @Override
    public List<Meal> selectLikeByName(String name) {
        return mealMapper.selectLikeByName(name);
    }

    /**
     * 查询指定类型菜品列表
     *
     * @param type 类型
     * @return 菜品列表
     */
    @Override
    public List<Meal> selectTypeQueryList(String type) {
        return mealMapper.selectTypeQueryList(type);
    }

    /**
     * 查询价格区间列表
     *
     * @param startPrice 开始价格
     * @param endPrice   结束价格
     * @return
     */
    @Override
    public List<Meal> selectPriceJoint(BigDecimal startPrice, BigDecimal endPrice) {
        return mealMapper.selectPriceJoint(startPrice,endPrice);
    }

    /**
     * 通过uid查询今日菜品数量
     *
     * @param uid 菜品uid
     * @return 数量
     */
    @Override
    public int selectDayNumber(String uid) {
        return mealMapper.selectDayNumber(uid);
    }

    /**
     * 修改餐品数据 乐观锁
     *
     * @param meal 餐品
     * @return 删除是否成功
     */
    @Override
    public boolean updateByUid(Meal meal) {
        //做lambdaQuery Select
        LambdaQueryWrapper<Meal> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Meal::getUid,meal.getUid());
        List<Meal> meals = mealMapper.selectList(lambdaQueryWrapper);
        //用LambdaUpdateWrapper 进行更新配置。
        Meal sqlMeals = meals.get(0);
        if(sqlMeals.getVersion() <0){
            return false;
        }
        meal.setVersion(sqlMeals.getVersion());
        //条件约束
        int versions = sqlMeals.getVersion() +1;
        return lambdaUpdate().eq(Meal::getUid,meal.getUid()).eq(Meal::getVersion,meal.getVersion()).set(Meal::getVersion,versions).update(meal);
    }

    /**
     * 删除餐品
     *
     * @param uid uid
     * @return 删除是否成功
     */
    @Override
    public boolean deleteByUid(String uid) {
        return mealMapper.deleteByUid(uid);
    }

    /**
     * 添加餐品
     *
     * @param meal
     * @return 是否成功
     */
    @Override
    public boolean addMeal(Meal meal) {
        meal.setUid(UUID.randomUUID().toString());
        return mealMapper.addMeal(meal)>0;
    }
}
